package com.tuo51.childcare.controller;

import com.alibaba.fastjson.JSON;
import com.tuo51.childcare.domain.bize.BizEvaluateQuestion;
import com.tuo51.childcare.domain.bize.BizEvaluateRecord;
import com.tuo51.childcare.domain.vo.BizEvaluateAnswerSheetItemVO;
import com.tuo51.childcare.service.BizEvaluateImportService;
import com.tuo51.childcare.service.BizEvaluateService;
import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.evaluate.service.ChildrenService;
import com.tuo51.evaluate.service.EvaluateV2Service;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 运营活动接口
 */

@RestController
@RequestMapping("/papi/marketing")
@Api(value = "/papi/marketing/", tags = "家长服务号 - 运营活动")
public class PAPIEvaluateMarketingController {

    @Autowired
    private BizEvaluateImportService importService;

    @Autowired
    private BizEvaluateService evaluateService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private ChildrenService childrenService;


    @Autowired
    private EvaluateV2Service evaluateV2Service;

//    @RequestMapping(value = "/children_list", method = POST)
//    @ApiOperation("家长侧小朋友列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false),
//
//    })
//    public StatusResponse childrenList(@RequestHeader(TICKET) String ticket){
//        List<EvaluateChildrenVO> evaluateChildrenVOS = evaluateV2Service.getChildrenEvaluateInfo(cache.getStaffId(ticket), null);
//        return StatusResponse.success(evaluateChildrenVOS);
//    }


    @RequestMapping(value = "/marketing_models", method = POST)
    @ApiOperation("运营活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false),

    })
    public StatusResponse marketingModels(@RequestHeader(TICKET) String ticket){
        return StatusResponse.success(evaluateService.allSupportingSheets());
    }

    @RequestMapping(value = "/marketing_questions", method = POST)
    @ApiOperation("运营活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false),
            @ApiImplicitParam(name = "modelId", value = "模型id", required = true)

    })
    public StatusResponse getMarketingQuestions(@RequestHeader(TICKET) String ticket,
                                               String modelId){

        List<BizEvaluateQuestion> questions = evaluateService.questionForSheet(modelId);

        return StatusResponse.success(questions);
    }

    @RequestMapping(value = "/marketing_conclusion", method = POST)
    @ApiOperation("运营活动的结论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header"),
            @ApiImplicitParam(name = "childrenId", value = "宝宝id, 指定id时忽略名字，性别生日", required = false),
            @ApiImplicitParam(name = "childrenName", value = "宝宝名字", required = false),
            @ApiImplicitParam(name = "gender", value = "性别", required = false),
            @ApiImplicitParam(name = "birthday", value = "生日", required = false),
            @ApiImplicitParam(name = "answerSheetJson", value = "答题卡", required = true, example = "JSON格式，如 [{'questionId':'1234', 'selections':[]}]"),
    })
    public StatusResponse getMarketingConclusion(@RequestHeader(TICKET) String ticket,
                                                 String childrenId,
                                                 String childrenName,
                                                 Integer gender,
                                                 String birthday,
                                                 String sheetId,
                                                 String answerSheetJson){

        List<BizEvaluateAnswerSheetItemVO> answerSheet = JSON.parseArray(answerSheetJson, BizEvaluateAnswerSheetItemVO.class);

        //获取用户
        Staff staff = cache.getStaff(ticket);

        //获取默认宝贝
        Children c = childrenService.defaultChildrenForParent(staff);

        if (StringUtils.isNotEmpty(childrenId)){
            c = childrenService.getById(childrenId, staff.getId());
            if (c == null){
                throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "指定宝贝不存在");
            }
            childrenName = c.getName();
            gender = c.getGender();
            birthday = DateUtil.formatDayToStr(c.getBirthday());
        }

        return StatusResponse.success(evaluateService.parseAnswerSheet(staff, childrenId, childrenName, gender, birthday, sheetId, answerSheet));

    }

    @RequestMapping(value = "/marketing_report", method = POST)
    @ApiOperation("运营活动报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false),
            @ApiImplicitParam(name = "recordId", value = "报告id", required = true)
    })
    public StatusResponse getMarketingReport(@RequestHeader(TICKET) String ticket,
                                                 String recordId){

        String staffId = cache.getStaffId(ticket);

        BizEvaluateRecord record = evaluateService.reportDetail(staffId, recordId);

        return StatusResponse.success(record);
    }

    @ApiOperation(value = "导入各种运营量表",httpMethod = "POST")
    @RequestMapping(value = "/import_question", method = RequestMethod.POST, headers="content-type=multipart/form-data")
    public StatusResponse importQuestion(@RequestHeader(TICKET) String ticket, @RequestParam(value = "file", required = false) MultipartFile file) {
        return StatusResponse.success(importService.importQuestion(file));
    }



    @RequestMapping(value = "/default_sheet_questions", method = POST)
    @ApiOperation("趣味测试, 获取sheetid和questions列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false)
    })
    public StatusResponse getDefaultSheetQuestions(@RequestHeader(TICKET) String ticket){
        Staff staff = cache.getStaff(ticket);

        Map result = evaluateService.defaultSheetQuestions(staff);

        return StatusResponse.success(result);
    }

    @RequestMapping(value = "/sheet_conclusion", method = POST)
    @ApiOperation("趣味测试的结论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header"),
            @ApiImplicitParam(name = "sheetId", value = "表单id", required = true),
            @ApiImplicitParam(name = "answerSheetJson", value = "答题卡", required = true, example = "JSON格式，如 [{'questionId':'1234', 'selections':[]}]"),
    })
    public StatusResponse getMarketingConclusion(@RequestHeader(TICKET) String ticket,
                                                 String sheetId,
                                                 String answerSheetJson){

        List<BizEvaluateAnswerSheetItemVO> answerSheet = JSON.parseArray(answerSheetJson, BizEvaluateAnswerSheetItemVO.class);

        //获取量表
        Staff staff = cache.getStaff(ticket);

        Children c = childrenService.defaultChildrenForParent(staff);

        return StatusResponse.success(evaluateService.parseAnswerSheet(staff, c.getId(), c.getName(), c.getGender(), null, sheetId, answerSheet));

    }


}
